package fact.view.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.sql.DataSource;

import fact.view.DTO.MakeDTO;
import fact.view.DTO.NodeHierarchyDTO;
import fact.view.DTO.ScenarioDTO;
import fact.view.DTO.UserDTO;




@ManagedBean(name = "nodeHierarchyModelBean")
//@RequestScoped
@ViewScoped
public class NodeHierarchyModelBean {
	
	@Resource(name="jdbc/factentry")
	private DataSource ds;
	
	public ArrayList<NodeHierarchyDTO> getNodeList(int segmentId) throws SQLException
	{
		ArrayList<NodeHierarchyDTO> nodeList=new ArrayList<NodeHierarchyDTO>();
		if(ds==null)
			throw new SQLException("Can't get data source");
		Connection con = ds.getConnection();
 
		if(con==null)
			throw new SQLException("Can't get database connection");
 
		PreparedStatement ps = con.prepareStatement("Select tmp2.*,MST_SEGMENTS.SEGMENT_NAME,NODE_TYPE.TYPE_NAME from  " +
				"(Select tmp1.* from  " +
				"( " +
				"SELECT Lpad(label,Length(label) + LEVEL * 10 - 10,'-') vizstring,label node_name,node_id,data_type,position,parent_node,node_type_id,segment_id,label,rownum indx " +
				"FROM   DB_FACTENTRY.Node_hierarchy " +
				"START WITH parent_node is null " +
				"CONNECT BY PRIOR  node_id=parent_node " +
				")tmp1 Where segment_id=? " +
				")tmp2,MST_SEGMENTS,NODE_TYPE " +
				"Where tmp2.NODE_TYPE_ID=NODE_TYPE.NODE_TYPE_ID " +
				"And   tmp2.SEGMENT_ID=MST_SEGMENTS.SEGMENT_ID " +
				"Order by tmp2.indx ");
		
		ps.setInt(1, segmentId);
		ResultSet result =  ps.executeQuery();
		
		NodeHierarchyDTO n=null;
				while(result.next())
				{
					n=new NodeHierarchyDTO();
					n.setNodeId(result.getInt("node_id"));
					n.setNodeName(result.getString("node_name"));
					n.setVizString(result.getString("vizstring"));
					n.setDataType(result.getString("data_type"));
					n.setParentNode(result.getInt("parent_node"));
					n.setNodeTypeId(result.getInt("node_type_id"));
					n.setSegmentId(result.getInt("segment_id"));
					n.setSegmentName(result.getString("SEGMENT_NAME"));
					n.setNodeTypeName(result.getString("TYPE_NAME"));
					n.setLabel(result.getString("LABEL"));
					
					
					nodeList.add(n);
				}
		
		
		return nodeList;
	}
	
	public ArrayList<NodeHierarchyDTO> getKpiNodeList(int segmentId) throws SQLException
	{
		ArrayList<NodeHierarchyDTO> nodeList=new ArrayList<NodeHierarchyDTO>();
		if(ds==null)
			throw new SQLException("Can't get data source");
		Connection con = ds.getConnection();
 
		if(con==null)
			throw new SQLException("Can't get database connection");
 
		PreparedStatement ps = con.prepareStatement("Select node_id,label from NODE_HIERARCHY Where Segment_Id=? and Node_Type_id=2 Order By Label");
		
		ps.setInt(1, segmentId);
		ResultSet result =  ps.executeQuery();
		
		NodeHierarchyDTO n=null;
				while(result.next())
				{
					n=new NodeHierarchyDTO();
					n.setNodeId(result.getInt("node_id"));
					n.setNodeName(result.getString("LABEL"));
					nodeList.add(n);
				}
		
		
		return nodeList;
	}
	
	public ArrayList<NodeHierarchyDTO> getAllKpiNodeList() throws SQLException
	{
		ArrayList<NodeHierarchyDTO> nodeList=new ArrayList<NodeHierarchyDTO>();
		if(ds==null)
			throw new SQLException("Can't get data source");
		Connection con = ds.getConnection();
 
		if(con==null)
			throw new SQLException("Can't get database connection");
 
		PreparedStatement ps = con.prepareStatement("Select node_id,label from NODE_HIERARCHY Where Node_Type_id=2 Order By Label");
		
		ResultSet result =  ps.executeQuery();
		
		NodeHierarchyDTO n=null;
				while(result.next())
				{
					n=new NodeHierarchyDTO();
					n.setNodeId(result.getInt("node_id"));
					n.setNodeName(result.getString("LABEL"));
					nodeList.add(n);
				}
		
		
		return nodeList;
	}
	

	
	public NodeHierarchyDTO getNode(int nodeId) throws SQLException
	{
		if(ds==null)
			throw new SQLException("Can't get data source");
		Connection con = ds.getConnection();
 
		if(con==null)
			throw new SQLException("Can't get database connection");
 
		PreparedStatement ps = con.prepareStatement("Select * from NODE_HIERARCHY  Where Node_id=?");
		
		ps.setInt(1, nodeId);
		ResultSet result =  ps.executeQuery();
		NodeHierarchyDTO n=null;
				if(result.next())
				{
					n=new NodeHierarchyDTO();
					n.setNodeId(result.getInt("node_id"));
					//n.setVizString(result.getString("vizstring"));
					n.setDataType(result.getString("data_type"));
					n.setParentNode(result.getInt("parent_node"));
					n.setNodeTypeId(result.getInt("node_type_id"));
					n.setSegmentId(result.getInt("segment_id"));
					n.setDescription(result.getString("DESCRIPTION"));
					//n.setSegmentName(result.getString("SEGMENT_NAME"));
					//n.setNodeTypeName(result.getString("TYPE_NAME"));
					n.setLabel(result.getString("LABEL"));
				}
		
		return n;
	}
	
	public boolean addNode(NodeHierarchyDTO node,ArrayList<MakeDTO> makeList,ArrayList<ScenarioDTO> scenarioList) throws SQLException
	{
		
		int nodeId=0;
		boolean operationResponse=false;
		if(ds==null)
			throw new SQLException("Can't get data source");
		Connection con = ds.getConnection();
		
 
		
		if(con==null)
			throw new SQLException("Can't get database connection");
 
		con.setAutoCommit(false);
		
		try
		{
		PreparedStatement ps = con.prepareStatement("Select SQN_NODE.NEXTVAL NODEID from dual");  
		ResultSet result =  ps.executeQuery();
				if(result.next())
					nodeId=result.getInt("NODEID");
		
		ps = con.prepareStatement("Insert into NODE_HIERARCHY " +
				"(NODE_ID,DATA_TYPE,LABEL,DESCRIPTION,POSITION,PARENT_NODE,NODE_TYPE_ID,SEGMENT_ID) " +
				" Values(?,?,?,?,?,?,?,?)");  
		
		ps.setInt(1, nodeId);
		ps.setString(2, node.getDataType());
		ps.setString(3, node.getLabel());
		ps.setString(4, node.getDescription());
		ps.setInt(5, 100);		
		
		if(node.getParentNode()==0)
			ps.setNull(6, java.sql.Types.NULL);
		else
		ps.setInt(6, node.getParentNode());
		
		ps.setInt(7, node.getNodeTypeId());
		ps.setInt(8, node.getSegmentId());
		
		ps.executeUpdate();
		
		if(node.getNodeTypeId()==2)
		{
			for(int i=0;i<makeList.size();i++)
			{
				for(int j=0;j<scenarioList.size();j++)
				{
					
					ps = con.prepareStatement("Insert into NODE_SCENARIO_ATTRIBUTES " +
							"(MAKE_ID,NODE_ID,SCENARIO_ID,MAX_VALUE,MIN_VALUE,VISIBILITY_FLAG) " +
							" Values(?,?,?,?,?,?)");  
					
					ps.setInt(1, makeList.get(i).getMakeId());
					ps.setInt(2, nodeId);
					ps.setInt(3, scenarioList.get(j).getScenarioId());
					ps.setInt(4, 999999);
					ps.setInt(5, 0);
					ps.setInt(6, 1);
					ps.executeUpdate();
					
				}
			}
		}
		con.commit();
		operationResponse=true;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			con.rollback();
		}
		
			return operationResponse;
		
	}
	
	public boolean deleteNode(int nodeId) throws SQLException
	{
		if(ds==null)
			throw new SQLException("Can't get data source");
		Connection con = ds.getConnection();
 
		if(con==null)
			throw new SQLException("Can't get database connection");
 
		PreparedStatement ps = con.prepareStatement("Delete NODE_HIERARCHY Where NODE_ID=?");  
		ps.setInt(1,nodeId);
		int result =  ps.executeUpdate();
		
		if(result==1)
			return true;
		else
			return false;
		
	}
	

	public boolean editNode(NodeHierarchyDTO node) throws SQLException
	{
		if(ds==null)
			throw new SQLException("Can't get data source");
		Connection con = ds.getConnection();
 
		if(con==null)
			throw new SQLException("Can't get database connection");
 
		PreparedStatement ps = con.prepareStatement("Update Node_Hierarchy Set Data_Type=?,Description=?,label=?,parent_Node=?, " +
				" Node_Type_Id=? Where Node_Id=?");  
		ps.setString(1, node.getDataType());
		ps.setString(2, node.getDescription());
		ps.setString(3, node.getLabel());
		
		if(node.getParentNode()==0)
			ps.setNull(4, java.sql.Types.NULL);
		else
		ps.setInt(4, node.getParentNode());
		
		ps.setInt(5, node.getNodeTypeId());
		ps.setInt(6, node.getNodeId());
		
		int result =  ps.executeUpdate();
		
		if(result==1)
			return true;
		else
			return false;
		
	}
	
	
		

}